package com.halley.serviceconsumer.Controller;

import com.halley.servicecontract.entity.User;
import com.halley.servicecontract.service.UserContract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class UserController {

    @Autowired
    private UserContract userContract;

    @PostMapping("/user")
    public void add(@RequestBody User user){
        userContract.add(user);
    }

    @GetMapping("/user/{name}")
    public User findByName(@PathVariable String name){
        Optional<User> userByName = userContract.findByName(name);
        return userByName.get();
    }
    @GetMapping("/users")
    public List<User> findAll(){
        List<User> all = userContract.findAll();
        return all;
    }
    @GetMapping("/findUserName/{username}")
    public String findUserName(@PathVariable String username){
        String userName = userContract.findUserName(username);
        return userName;
    }
}
